{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-11T06:36:21.777273600Z",
     "start_time": "2024-01-11T06:36:21.763275600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name John <class 'str'>\n",
      "age 20 <class 'int'>\n",
      "address None <class 'NoneType'>\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'Person' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[15], line 18\u001B[0m\n\u001B[0;32m     15\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m k,v \u001B[38;5;129;01min\u001B[39;00m p:\n\u001B[0;32m     16\u001B[0m     \u001B[38;5;28mprint\u001B[39m(k,v,\u001B[38;5;28mtype\u001B[39m(v))\n\u001B[1;32m---> 18\u001B[0m \u001B[43mp\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mage\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m]\u001B[49m \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m3\u001B[39m\n",
      "\u001B[1;31mTypeError\u001B[0m: 'Person' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "from pydantic import BaseModel\n",
    "\n",
    "\n",
    "class Person(BaseModel):\n",
    "    name: str\n",
    "    age: int\n",
    "    address: str | None = None\n",
    "\n",
    "    @property\n",
    "    def upper_name(self):\n",
    "        return self.name.upper()\n",
    "\n",
    "\n",
    "class PersonVO(Person):\n",
    "    upper_name: str = None\n",
    "\n",
    "\n",
    "p = Person(name='John', age=20)\n",
    "for k, v in p:\n",
    "    print(k, v, type(v))\n",
    "\n",
    "p['age'] = 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2024, 1, 11, 15, 6, 12)"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "datetime.fromtimestamp(int(time.time()))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-11T07:06:12.560708700Z",
     "start_time": "2024-01-11T07:06:12.556710100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Hello, this is the first text.\n"
     ]
    }
   ],
   "source": [
    "import difflib\n",
    "from difflib import HtmlDiff\n",
    "\n",
    "text1 = \"Hello, this is the first text.\"\n",
    "text2 = \"Hello, this is the first text.\"\n",
    "r = difflib.Differ().compare([text1], [text2])\n",
    "for x in r:\n",
    "    print(x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T11:38:27.526958600Z",
     "start_time": "2024-01-12T11:38:27.524463600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('12', '15', 'cannot find symbol'), ('12', '37', 'cannot find symbol'), ('16', '9', 'cannot find symbol'), ('20', '22', 'cannot find symbol'), ('26', '15', 'cannot find symbol'), ('26', '37', 'cannot find symbol'), ('30', '9', 'cannot find symbol'), ('39', '15', 'cannot find symbol'), ('39', '37', 'cannot find symbol'), ('43', '9', 'cannot find symbol')]\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "file_path = r\"D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java\"\n",
    "\n",
    "error_log = \"\"\"\n",
    "Test Code Compile error!\n",
    " [ERROR] COMPILATION ERROR :\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[12,15] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[12,37] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[16,9] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[20,22] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[26,15] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[26,37] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[30,9] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[39,15] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[39,37] cannot find symbol\n",
    "[ERROR] /D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:[43,9] cannot find symbol\n",
    "\"\"\"\n",
    "\n",
    "results = re.findall(rf\"{re.escape(file_path)}:\\[(\\d+),(\\d+)\\] (.*?)$\", error_log, re.MULTILINE)\n",
    "\n",
    "print(results)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T13:11:42.320408900Z",
     "start_time": "2024-01-12T13:11:42.307955200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "outputs": [
    {
     "data": {
      "text/plain": "[('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol'),\n ('TestEnum', 'cannot find symbol')]"
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "\n",
    "file_path = r\"D:/IDEA_Projects/TestJavaCode/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java\"\n",
    "\n",
    "with open(file_path, 'r', encoding='utf-8') as file:\n",
    "    code_lines = file.readlines()\n",
    "\n",
    "results = [('12', '15', 'cannot find symbol'), ('12', '37', 'cannot find symbol'), ('16', '9', 'cannot find symbol'),\n",
    "           ('20', '22', 'cannot find symbol'), ('26', '15', 'cannot find symbol'), ('26', '37', 'cannot find symbol'),\n",
    "           ('30', '9', 'cannot find symbol'), ('39', '15', 'cannot find symbol'), ('39', '37', 'cannot find symbol'),\n",
    "           ('43', '9', 'cannot find symbol')]\n",
    "\n",
    "# 提取代码内容\n",
    "errors = []\n",
    "for line, loc, reason in results:\n",
    "    line = int(line) - 1\n",
    "    loc = int(loc) - 1\n",
    "    code_line = code_lines[line]\n",
    "    new_code_line = code_line\n",
    "    for i in range(loc, len(code_line)):\n",
    "        if not code_line[i].isalpha():\n",
    "            new_code_line = code_line[loc:i]\n",
    "            break\n",
    "    errors.append((new_code_line, reason))\n",
    "\n",
    "errors\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T13:05:50.364554800Z",
     "start_time": "2024-01-12T13:05:50.357055Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Class<TestEnum> enumClass = TestEnum.class;\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'declarations'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[68], line 11\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(code\u001B[38;5;241m.\u001B[39mstrip())\n\u001B[0;32m      4\u001B[0m tree \u001B[38;5;241m=\u001B[39m javalang\u001B[38;5;241m.\u001B[39mparse\u001B[38;5;241m.\u001B[39mparse(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\"\"\u001B[39m\n\u001B[0;32m      5\u001B[0m \u001B[38;5;124mpublic class Temp \u001B[39m\u001B[38;5;130;01m{{\u001B[39;00m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;124m    void example() \u001B[39m\u001B[38;5;130;01m{{\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;130;01m}}\u001B[39;00m\n\u001B[0;32m     10\u001B[0m \u001B[38;5;124m\"\"\"\u001B[39m)\n\u001B[1;32m---> 11\u001B[0m t \u001B[38;5;241m=\u001B[39m \u001B[43mtree\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mtypes\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmethods\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbody\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdeclarations\u001B[49m[\u001B[38;5;241m0\u001B[39m]\n\u001B[0;32m     12\u001B[0m t\n",
      "\u001B[1;31mAttributeError\u001B[0m: 'list' object has no attribute 'declarations'"
     ]
    }
   ],
   "source": [
    "import javalang\n",
    "\n",
    "code = errors[0][0]\n",
    "print(code.strip())\n",
    "tree = javalang.parse.parse(f\"\"\"\n",
    "public class Temp {{\n",
    "    void example() {{\n",
    "    {code.strip()}\n",
    "    }}\n",
    "}}\n",
    "\"\"\")\n",
    "t = tree.types[0].methods[0].body.declarations[0]\n",
    "t"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T12:37:09.443730900Z",
     "start_time": "2024-01-12T12:37:09.435732200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "outputs": [
    {
     "data": {
      "text/plain": "['test1', 'test2', 'test3']"
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "origin_code = \"\"\"\n",
    "package org.apache.commons.lang3;\n",
    "import java.util.*;\n",
    "import static org.junit.Assert.*;\n",
    "import org.junit.Test;\n",
    "\n",
    "public class ValidateTest {\n",
    "\n",
    "    @Test\n",
    "    public void test1() {\n",
    "        // Test case 1: expression is true\n",
    "        try {\n",
    "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
    "        } catch (IllegalArgumentException e) {\n",
    "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
    "        }\n",
    "\n",
    "        // Test case 2: expression is false\n",
    "        try {\n",
    "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
    "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
    "        } catch (IllegalArgumentException e) {\n",
    "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
    "        }\n",
    "    }\n",
    "    @Test\n",
    "    public void test2() {\n",
    "        // Test case 1: expression is true\n",
    "        try {\n",
    "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
    "        } catch (IllegalArgumentException e) {\n",
    "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
    "        }\n",
    "\n",
    "        // Test case 2: expression is false\n",
    "        try {\n",
    "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
    "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
    "        } catch (IllegalArgumentException e) {\n",
    "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
    "        }\n",
    "    }\n",
    "    @Test\n",
    "    public void test3() {\n",
    "        // Test case 1: expression is true\n",
    "        try {\n",
    "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
    "        } catch (IllegalArgumentException e) {\n",
    "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
    "        }\n",
    "\n",
    "        // Test case 2: expression is false\n",
    "        try {\n",
    "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
    "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
    "        } catch (IllegalArgumentException e) {\n",
    "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "import re\n",
    "\n",
    "r = re.findall(\"@Test.*\\n.*public void (\\w+)\\(\\)\", origin_code)\n",
    "r"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T05:57:44.200470700Z",
     "start_time": "2024-01-13T05:57:44.186972200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    @Test\n",
      "    public void test2() {\n",
      "        // Test case 1: expression is true\n",
      "        try {\n",
      "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
      "        }\n",
      "\n",
      "        // Test case 2: expression is false\n",
      "        try {\n",
      "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
      "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
      "        }\n",
      "    }\n"
     ]
    }
   ],
   "source": [
    "def get_testcase(code: str, method_name: str):\n",
    "    lines = code.splitlines()\n",
    "    for start, line in enumerate(lines):\n",
    "        if f\"public void {method_name}(\" in line:\n",
    "            end = start + 1\n",
    "            stack1 = [\"{\"]\n",
    "            while len(stack1) != 0:\n",
    "                end += 1\n",
    "                line = lines[end - 1]\n",
    "                for ch in line:\n",
    "                    if ch == \"{\":\n",
    "                        stack1.append(ch)\n",
    "                    elif ch == \"}\":\n",
    "                        stack1.pop()\n",
    "                if end > 10000:\n",
    "                    assert False\n",
    "            return \"\\n\".join(lines[start - 1:end])\n",
    "        #end if\n",
    "    #end for\n",
    "\n",
    "\n",
    "result = get_testcase(origin_code, 'test2')\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:08:05.477366400Z",
     "start_time": "2024-01-13T06:08:05.475366400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "package org.apache.commons.lang3;\n",
      "import java.util.*;\n",
      "import static org.junit.Assert.*;\n",
      "import org.junit.Test;\n",
      "\n",
      "public class ValidateTest {\n",
      "\n",
      "    @Test\n",
      "    public void test1() {\n",
      "        // Test case 1: expression is true\n",
      "        try {\n",
      "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
      "        }\n",
      "\n",
      "        // Test case 2: expression is false\n",
      "        try {\n",
      "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
      "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
      "        }\n",
      "    }\n",
      "    @Test\n",
      "    public void test2() {\n",
      "        // Test case 1: expression is true\n",
      "        try {\n",
      "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
      "        }\n",
      "\n",
      "        // Test case 2: expression is false\n",
      "        try {\n",
      "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
      "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
      "        }\n",
      "    }\n",
      "    @Test\n",
      "    public void test3() {\n",
      "        // Test case 1: expression is true\n",
      "        try {\n",
      "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
      "        }\n",
      "\n",
      "        // Test case 2: expression is false\n",
      "        try {\n",
      "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
      "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
      "        }\n",
      "    }\n",
      "}\n",
      " \n",
      "package org.apache.commons.lang3;\n",
      "import java.util.*;\n",
      "import static org.junit.Assert.*;\n",
      "import org.junit.Test;\n",
      "\n",
      "public class ValidateTest {\n",
      "\n",
      "    @Test\n",
      "    public void test1() {\n",
      "        // Test case 1: expression is true\n",
      "        try {\n",
      "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
      "        }\n",
      "\n",
      "        // Test case 2: expression is false\n",
      "        try {\n",
      "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
      "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
      "        }\n",
      "    }\n",
      "    @Test\n",
      "    public void test3() {\n",
      "        // Test case 1: expression is true\n",
      "        try {\n",
      "            Validate.isTrue(true, \"The value must be greater than zero: %s\", 5.0);\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            fail(\"Test case 1 failed: \" + e.getMessage());\n",
      "        }\n",
      "\n",
      "        // Test case 2: expression is false\n",
      "        try {\n",
      "            Validate.isTrue(false, \"The value must be greater than zero: %s\", 3.0);\n",
      "            fail(\"Test case 2 failed: Expected IllegalArgumentException\");\n",
      "        } catch (IllegalArgumentException e) {\n",
      "            assertEquals(\"The value must be greater than zero: 3.0\", e.getMessage());\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "from typing import List\n",
    "\n",
    "\n",
    "def remove_testcase(code: str, origin_names: List[str], new_names: List[str]):\n",
    "    lines = code.splitlines()\n",
    "    duplicate_names = set(origin_names) & set(new_names)\n",
    "    for name in duplicate_names:\n",
    "        for start, line in enumerate(lines):\n",
    "            if f\"public void {name}(\" in line:\n",
    "                end = start + 1\n",
    "                stack1 = [\"{\"]\n",
    "                while len(stack1) != 0:\n",
    "                    end += 1\n",
    "                    line = lines[end - 1]\n",
    "                    for ch in line:\n",
    "                        if ch == \"{\":\n",
    "                            stack1.append(ch)\n",
    "                        elif ch == \"}\":\n",
    "                            stack1.pop()\n",
    "                    if end > 10000:\n",
    "                        assert False\n",
    "                lines = lines[:start - 1] + lines[end:]\n",
    "            # end if\n",
    "    # end for\n",
    "    return \"\\n\".join(lines)\n",
    "\n",
    "\n",
    "result = remove_testcase(origin_code, ['test1', 'test2'], ['test2', 'test3'])\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T06:17:44.405486600Z",
     "start_time": "2024-01-13T06:17:44.399487200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "package org.apache.commons.lang3;\n",
      "import java.util.*;\n",
      "import org.junit.Test;\n",
      "import static org.junit.Assert.*;\n",
      "\n",
      "public class ValidateTest {\n",
      "\n",
      "    @Test\n",
      "    public void testIsInstanceOf_validInstance() {\n",
      "        try{\n",
      "        Object obj = \"test\";\n",
      "        if(a > 6){\n",
      "          \n",
      "        }\n",
      "        assertEquals(1,2);\n",
      "        }catch(NullPointerException e){\n",
      "        \n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "from javalang.tree import MethodDeclaration, TryStatement\n",
    "from typing import List\n",
    "import javalang\n",
    "_t = None\n",
    "import re\n",
    "\n",
    "def modify_exception_type(test_code: str, error_loc: str | int, exception_type: str):\n",
    "    code_lines = test_code.splitlines()\n",
    "    error_loc = int(error_loc)\n",
    "\n",
    "    def get_method_name_by_loc(methods: List[MethodDeclaration]):\n",
    "        for i in range(len(methods)):\n",
    "            method1 = methods[i]\n",
    "            method2 = methods[i + 1] if i + 1 < len(methods) else None\n",
    "            if method2 is None or method1.position[0] <= error_loc < method2.position[0]:\n",
    "                return method1.name\n",
    "\n",
    "    def get_try_block_by_loc(try_blocks: List[TryStatement]):\n",
    "        for i in range(len(try_blocks)):\n",
    "            try1 = try_blocks[i]\n",
    "            try2 = try_blocks[i + 1] if i + 1 < len(try_blocks) else None\n",
    "            if try1.position[0] > error_loc:\n",
    "                return None\n",
    "\n",
    "            if try2 is None or try1.position[0] <= error_loc < try2.position[0]:\n",
    "                return try1\n",
    "\n",
    "    def get_catch_position(start: int, types: List[str]):\n",
    "        \"\"\"\n",
    "        :param start: 起始行下表，表示从哪一行开始找\n",
    "        :param types: 目标类型，例：['IllegalArgumentException', 'NumberFormatException'] 表示找到 catch(IllegalArgumentException | NumberFormatException e)\n",
    "        :return: 目标行的下标\n",
    "        \"\"\"\n",
    "        for i in range(start, len(code_lines)):\n",
    "            line = code_lines[i]\n",
    "            flag = 'catch' in line\n",
    "            for t in types:\n",
    "                flag = flag and t in line\n",
    "            if flag:\n",
    "                return i\n",
    "\n",
    "    tree = javalang.parse.parse(test_code)\n",
    "    methods = tree.types[0].methods\n",
    "    target_method = get_method_name_by_loc(methods)\n",
    "    body = [x.body for x in methods if target_method == x.name]\n",
    "    assert len(body) == 1\n",
    "    body = body[0]\n",
    "    try_blocks = [x for x in body if isinstance(x, TryStatement)]\n",
    "\n",
    "    if try_blocks is None or len(try_blocks) == 0:\n",
    "        return test_code\n",
    "\n",
    "    try_block = get_try_block_by_loc(try_blocks)\n",
    "    if try_block is None:\n",
    "        return test_code\n",
    "\n",
    "    catch_loc = get_catch_position(try_block.position[0], try_block.catches[0].parameter.types)\n",
    "    code_lines[catch_loc] = re.sub(\"catch.*\\((.*) .*\\)\", lambda x: x.group(0).replace(x.group(1), exception_type),\n",
    "                                   code_lines[catch_loc], count=1)\n",
    "    for i in range(try_block.position[0], catch_loc):\n",
    "        code_lines[i] = re.sub('\\\".*(IllegalArgumentException).*\\\"',\n",
    "                               lambda x: x.group(0).replace(x.group(1), exception_type), code_lines[i], count=1)\n",
    "    return \"\\n\".join(code_lines)\n",
    "\n",
    "test_code = \"\"\"\n",
    "package org.apache.commons.lang3;\n",
    "import java.util.*;\n",
    "import org.junit.Test;\n",
    "import static org.junit.Assert.*;\n",
    "\n",
    "public class ValidateTest {\n",
    "\n",
    "    @Test\n",
    "    public void testIsInstanceOf_validInstance() {\n",
    "        try{\n",
    "        Object obj = \"test\";\n",
    "        if(a > 6){\n",
    "          \n",
    "        }\n",
    "        assertEquals(1,2);\n",
    "        }catch(Exception e){\n",
    "        \n",
    "        }\n",
    "    }\n",
    "}\n",
    "\"\"\"\n",
    "_ = modify_exception_type(test_code, 15, \"NullPointerException\")\n",
    "print(_)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-22T10:06:23.724386600Z",
     "start_time": "2024-01-22T10:03:05.666541800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "outputs": [
    {
     "data": {
      "text/plain": "('woudnoa awoudnaouwdnoauwbn doauwbdn ouawb',\n 'woudnoa awoudnaouwdn auwbn doauwbdn ouawb')"
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "text = \"\"\"\n",
    "[ERROR] Failures:\n",
    "[ERROR]   ValidateTest.fix9:58 expected:<...woudnoa awoudnaouwdn[o]auwbn doauwbdn ouawb...> but was:<...woudnoa awoudnaouwdn[ ]auwbn doauwbdn ouawb...>\n",
    "\"\"\"\n",
    "result = re.search(\"expected:(.*) but was:(.*)\", text)\n",
    "expected = None\n",
    "was = None\n",
    "if result:\n",
    "    expected = result.group(1).strip()\n",
    "    was = result.group(2).strip()\n",
    "def replace_brackets(string: str):\n",
    "    if string.startswith(\"<[\"):\n",
    "        string = string.replace(\"<[\", \"\").replace(\"]>\", \"\")\n",
    "    if string.startswith(\"<...\"):\n",
    "        string = string.replace(\"<...\", \"\").replace(\"...>\", \"\")\n",
    "    if string.startswith(\"<\"):\n",
    "        string = string.replace(\"<\", \"\").replace(\">\", \"\")\n",
    "    string = string.replace(\"[\", \"\").replace(\"]\", \"\")\n",
    "    return string\n",
    "expected = replace_brackets(expected)\n",
    "was = replace_brackets(was)\n",
    "if was.startswith(\"java.lang.Integer\"):\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T09:25:06.228397700Z",
     "start_time": "2024-01-15T09:25:06.222881300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "text = \"<12[3]3>\"\n",
    "pattern = r\"<.*\\[(.*)\\].*>\"\n",
    "new_text = re.sub(pattern, r\"\\1\", text)\n",
    "print(new_text)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T09:01:43.796586900Z",
     "start_time": "2024-01-15T09:01:43.792587500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "public class BooleanUtils {\n",
      "\n",
      "    \n",
      "    public BooleanUtils() {\n",
      "      super();\n",
      "    }\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "def remove_comments(input_string):\n",
    "    pattern = r'/\\*.*?\\*/'\n",
    "    return re.sub(pattern, '', input_string, flags=re.DOTALL)\n",
    "\n",
    "input_string = \"\"\"\n",
    "/**\n",
    " * <p>Operations on boolean primitives and Boolean objects.</p>\n",
    " *\n",
    " * <p>This class tries to handle {@code null} input gracefully.\n",
    " * An exception will not be thrown for a {@code null} input.\n",
    " * Each method documents its behaviour in more detail.</p>\n",
    " *\n",
    " * <p>#ThreadSafe#</p>\n",
    " * @since 2.0\n",
    " * @version $Id$\n",
    " */\n",
    "public class BooleanUtils {\n",
    "\n",
    "    /**\n",
    "     * <p>{@code BooleanUtils} instances should NOT be constructed in standard programming.\n",
    "     * Instead, the class should be used as {@code BooleanUtils.negate(true);}.</p>\n",
    "     *\n",
    "     * <p>This constructor is public to permit tools that require a JavaBean instance\n",
    "     * to operate.</p>\n",
    "     */\n",
    "    public BooleanUtils() {\n",
    "      super();\n",
    "    }\n",
    "\"\"\"\n",
    "\n",
    "output_string = remove_comments(input_string)\n",
    "print(output_string)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-22T10:42:25.201467600Z",
     "start_time": "2024-01-22T10:42:25.187740900Z"
    }
   },
   "execution_count": 12
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
